package cn.yeziji.forum.dto.pay.wechat;

import cn.hutool.core.annotation.Alias;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.yeziji.forum.dto.pay.merchant.MerchantWechatPayInfoDTO;
import cn.yeziji.forum.dto.pay.path.WechatPathDTO;
import com.ijpay.core.IJPayHttpResponse;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * wechat V3 平台证书列表 数据对象
 *
 * @author hwy
 * @since 2023/07/30 3:54
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WechatV3CertificatesDTO {
  /**
   * 平台证书序列号
   */
  private String certificateSerialNumber;

  /**
   * 序列号
   */
  private String serialNumber;

  /**
   * 加密信息
   */
  @Alias("encrypt_certificate")
  private EncryptCertificate encryptCertificate;

  /**
   * 商户微信支付信息
   */
  private MerchantWechatPayInfoDTO merchantWechatPayInfo;

  /**
   * 微信目录信息
    */
  private WechatPathDTO wechatPathInfo;

  public WechatV3CertificatesDTO(String serialNumber, EncryptCertificate encryptCertificate) {
    this.serialNumber = serialNumber;
    this.encryptCertificate = encryptCertificate;
  }

  public WechatV3CertificatesDTO(IJPayHttpResponse response) {
    String body = response.getBody();
    // 解析 response body
    JSONObject jsonObject = JSONUtil.parseObj(body);
    JSONArray dataArray = jsonObject.getJSONArray("data");
    JSONObject data = dataArray.getJSONObject(0);
    JSONObject encryptCertificate = data.getJSONObject("encrypt_certificate");
    this.serialNumber = response.getHeader("Wechatpay-Serial");
    this.encryptCertificate = JSONUtil.toBean(encryptCertificate, EncryptCertificate.class);
  }

  public static WechatV3CertificatesDTO buildPayInfo(IJPayHttpResponse response, MerchantWechatPayInfoDTO merchantWechatPayInfoDTO, WechatPathDTO wechatPathDTO) {
    WechatV3CertificatesDTO wechatV3CertificatesDTO = new WechatV3CertificatesDTO(response);
    wechatV3CertificatesDTO.setMerchantWechatPayInfo(merchantWechatPayInfoDTO);
    wechatV3CertificatesDTO.setWechatPathInfo(wechatPathDTO);
    return wechatV3CertificatesDTO;
  }

  public String getAssociatedData() {
    return this.encryptCertificate.getAssociatedData();
  }

  public String getCiphertext() {
    return this.encryptCertificate.getCiphertext();
  }

  public String getNonce() {
    return this.encryptCertificate.getNonce();
  }

  public String getApiKeyV3() {
    return this.merchantWechatPayInfo.getApiKeyV3();
  }

  public void setPlatformCertPath(String platformCertPath) {
    if (this.wechatPathInfo == null) {
      throw new IllegalArgumentException("必须先构造微信 pathInfo");
    }
    this.wechatPathInfo.setPlatformCertPath(platformCertPath);
  }

  public String getPlatformCertPath() {
    if (this.wechatPathInfo == null) {
      throw new IllegalArgumentException("必须先构造微信 pathInfo");
    }
    return this.wechatPathInfo.getPlatformCertPath();
  }

  @Data
  static class EncryptCertificate {
    @Alias("associated_data")
    private String associatedData;

    private String ciphertext;

    private String nonce;
  }
}
